package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityUserDigFloor;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorCellData;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.DigFloorActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityDigModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.DigMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
@Cmd(code = GameProtocol.S_ACTIVITY_DIG, desc = "挖宝库")
public class DigActivityFloorDataCmd extends AbstractCommand {

    int activityId;
    int digId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DigFloorActivityProto.DigFloorReqMsg msg = DigFloorActivityProto.DigFloorReqMsg.parseFrom(packet.getBytes());
        digId = msg.getDigId();
        activityId = msg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = DigMgr.getActivityInfo(activityId);
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_HUA_SHI_GAG_NOT_IN_FIGHT_TIME;
            return;
        }
        int boxId = 0;
        if(activityInfo.getType() == eActivityType.PeakStrongHold.getValue()){
            boxId = PeakStrongholdActivityMgr.getConfig().getDesertPassItem();
        }else{
            boxId = StrongholdActivityMgr.getDesertPassItem();
        }

        ActivityDigModule activityDigModule = player.getModule(ActivityDigModule.class);
        activityDigModule.checkFloor(activityInfo, boxId);

        ActivityUserDigFloor activityUserDigFloor = activityDigModule.getFloorData(activityInfo.getActivityId());
        HuaShiGangFloorCellData cellData = activityUserDigFloor.getCellDataList().get(digId - 1);
        if (cellData.isOpen()) {
            ret = GameErrorCode.E_HUA_SHI_GANG_FLOOR_CELL_OPEN;
            return;
        }

        CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
        HuaShiGangFloorConfig config = DigMgr.getFloorConfigMap(activityInfo.getActivityId(), activityUserDigFloor.getFloor());
        if (!currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(config.getConsume()), eLogMoneyType.DesertTreasure, eLogMoneyType.DesertTreasureDig)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        // 设置打开状态
        cellData.setOpen(true);
        activityUserDigFloor.setUpdateOption();

        // 获得奖励
        Property property = PropertyHelper.parseStringToProperty(cellData.getReward());
        currencyModule.addCurrency(property, eLogMoneyType.DesertTreasure, eLogMoneyType.DesertTreasureDig);

        // 判断是否进入下一层 进入下一层则有通关大奖
        property.addProperty(activityDigModule.checkFloor(activityInfo, boxId));

        DigFloorActivityProto.DigFloorRespMsg.Builder builder = DigFloorActivityProto.DigFloorRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setReward(PropertyHelper.parsePropertyToString(property));
        builder.setSpecial(cellData.isSpecial());

        player.sendPacket(ClientProtocol.U_ACTIVITY_DIG, builder);
    }


    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DigFloorActivityProto.DigFloorRespMsg.Builder builder = DigFloorActivityProto.DigFloorRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_ACTIVITY_DIG, builder);
        }
    }
}
